/**
 * @(#) PacdpMediator.as
 *
 * @date		2012. 09. 19.
 * @version		1.0
 * @author		Park, Jutae
 * @description	CDP신청 - Mediator
 * 				CDP를 조회하고 신청하는 Presentation Logic을 관리 
 */
package com.skt.common_hr.pacdp.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.skt.common_hr.pacdp.PacdpFacade;
	import com.skt.common_hr.pacdp.model.PacdpProxy;
	import com.skt.common_hr.pacdp.model.vo.PacdpVO;
	import com.skt.common_hr.pacdp.view.components.PacdpView;
	import com.skt.common_hr.pacdp.view.components.ReqPacdp;
	
	import flash.events.Event;
	
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class PacdpMediator extends OverViewMediator
	{
		public static const NAME:String = "PacdpMediator"; 


		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function PacdpMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		override protected function activatedView():void
 		{
 			loadPacdpList();
 			setEventListeners();
 		}
 		
 		override protected function closedView():void
 		{
			removeEventListeners(); 			
 		}
 		
 		private function setEventListeners():void
 		{
 			view.addEventListener(PacdpView.InsertPacdp, onNewCdp);
 			view.addEventListener(PacdpView.UpdatePacdp, onUpdateCdp);
 			view.addEventListener(PacdpView.DeletePacdp, onDelCdp);
 			view.addEventListener(PacdpView.dgClickPacdp, onDgClick);
 			
 		}
 		
 		private function removeEventListeners():void
 		{
 			view.removeEventListener(PacdpView.InsertPacdp, onNewCdp);
 			view.removeEventListener(PacdpView.UpdatePacdp, onUpdateCdp);
 			view.removeEventListener(PacdpView.DeletePacdp, onDelCdp);
 			view.removeEventListener(PacdpView.dgClickPacdp, onDgClick);
 		}
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return basicInterestMessages.concat([
					PacdpFacade.LOAD_LIST_COMPLETE,
					PacdpFacade.LOAD_LIST_FAILED,
					PacdpFacade.INSERT_COMPLETE,
					PacdpFacade.INSERT_FAILED,
					PacdpFacade.UPDATE_COMPLETE,
					PacdpFacade.UPDATE_FAILED,
					PacdpFacade.DELETE_COMPLETE,
					PacdpFacade.DELETE_FAILED,
					PacdpFacade.READ_LIST_COMPLETE,
					PacdpFacade.READ_LIST_FAILED,
					PacdpFacade.OK_REQUEST_INPUT,
					PacdpFacade.CANCEL_REQUEST_INPUT
					]);
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName()) 
			{
				case PacdpFacade.LOAD_LIST_COMPLETE:
					onLoadPacdpList(note.getBody() as PacdpProxy);
					break;
				case PacdpFacade.LOAD_LIST_FAILED:
					break;
				case PacdpFacade.READ_LIST_COMPLETE:
					onReadList(note.getBody() as PacdpProxy);
					break;
				case PacdpFacade.READ_LIST_FAILED:
					break;	
				case PacdpFacade.OK_REQUEST_INPUT:
					mx.managers.PopUpManager.removePopUp(view.popup);
					loadPacdpList();
				case PacdpFacade.CANCEL_REQUEST_INPUT:
					facade.removeMediator(ReqPacdpMediator.NAME);
					mx.managers.PopUpManager.removePopUp(view.popup);
					view.popup = new ReqPacdp;
					//view.currentState = "";
					break;
				case PacdpFacade.DELETE_COMPLETE:
					loadPacdpList();
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		
		
		
 		private function onLoadPacdpList(proxy:PacdpProxy):void
 		{
 			view.pacdpColl 				= proxy.pacdpList;
 			view.popup.cbList 			= proxy.cbList;
 			view.popup.sinChungYear 	= proxy.sinChungYear;
 			view.popup.sinChungName 	= proxy.sinChungName;
 			view.popup.sRettext 		= proxy.sRettext;
 			
 		}
 		
 		private function onReadList(proxy:PacdpProxy):void
 		{
 			var vo:PacdpVO = view.dg.selectedItem as PacdpVO;
 			view.popup.sDvPop = view.sDv;
 			
 			facade.registerMediator(new ReqPacdpMediator(view.popup, vo));
 			
 		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private function onNewCdp(event:Event):void
		{
//			var _obj:PacdpVO = view.pacdpColl.getItemAt(0) as PacdpVO;
			
			view.popup.sDvPop = view.sDv;
			facade.registerMediator(new ReqPacdpMediator(view.popup, null));
		}

		private function onUpdateCdp(event:Event):void
		{
			var obj:PacdpVO = view.dg.selectedItem as PacdpVO;
 			view.popup.sDvPop = view.sDv;
 			
 			facade.registerMediator(new ReqPacdpMediator(view.popup, obj));
 			
		}

		private function onDelCdp(event:Event):void
		{
			
 			var proxy:PacdpProxy = facade.retrieveProxy(PacdpProxy.NAME) as PacdpProxy;
 			proxy.del(Variables.myPernr); 
			
		}
		
		private function onDgClick(event:Event):void
		{
			var proxy:PacdpProxy = facade.retrieveProxy(PacdpProxy.NAME) as PacdpProxy;
 			proxy.readLoad(Variables.myPernr);
		}

 		private function loadPacdpList():void 
 		{
 			var proxy:PacdpProxy = facade.retrieveProxy(PacdpProxy.NAME) as PacdpProxy;
 			proxy.load(Variables.myPernr);
 		}
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():PacdpView
        {
            return viewComponent as PacdpView;
        }
		
		
	}
}